﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using STOCK.Model;

namespace STOCK.DAL
{
    public class T_FormulaCategoryDAL
    {
        public T_FormulaCategory ToModel(DataRow row)
        {
            T_FormulaCategory item=new  T_FormulaCategory();
            item.Id=(Guid)SqlHelper.FromDbValue(row["Id"]);           
            item.Name=(String)SqlHelper.FromDbValue(row["Name"]);           
            return item;
        }
        private T_FormulaCategory[] ToT_FormulaCategorys(DataTable table)
        {
            T_FormulaCategory[] items=new T_FormulaCategory[table.Rows.Count];
            for (int i=0;i<table.Rows.Count;i++)
            {
                items[i]=ToModel(table.Rows[i]);
            }
            return items;
        }
        
        public T_FormulaCategory[] ListAll()
        {
            DataTable table=SqlHelper.ExecuteDataTable("Select * from T_FormulaCategory");
            return ToT_FormulaCategorys(table);
        }
        public void Update(T_FormulaCategory item)
        {
            SqlHelper.ExecuteNonQuery(@"Update T_FormulaCategory 
                    set Id=@Id
			,Name=@Name
                    where Id=@Id
			,Name=@Name" 
			,new SqlParameter("@Id",item.Id)
			,new SqlParameter("@Name",item.Name));            
        }
        public void Insert(T_FormulaCategory item)
        {
             SqlHelper.ExecuteNonQuery(@"INSERT INTO T_FormulaCategory
                (Id,Name)
                Values(@Id,@Name)" 
			,new SqlParameter("@Id",item.Id)
			,new SqlParameter("@Name",item.Name));
        }
        public T_FormulaCategory GetById(Guid id)
        {
            DataTable table = SqlHelper.ExecuteDataTable("select * from T_FormulaCategory where Id=@Id",
                new SqlParameter("@Id",id));
            if (table.Rows.Count <= 0)
            {
                return null;
            }
            else if (table.Rows.Count == 1)
            {
                return ToModel(table.Rows[0]);
            }
            else
            {
                throw new Exception();
            }
        }
    }
}
